package com.zhou.kotlin_car_data_list.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.BindingAdapter
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.zhou.kotlin_car_data_list.R
import com.zhou.kotlin_car_data_list.bean.Brand
import com.zhou.kotlin_car_data_list.databinding.ItemLogoBinding
import com.zhou.kotlin_car_data_list.ui.BrandListActivity
import com.zhou.kotlin_car_data_list.ui.LogoActivity

/**
 * </br>
 * Date: 2025/8/24
 * Created by:zhourui
 */
class LogoAdapter(var context: Context, var mData: MutableList<Brand>) :
    RecyclerView.Adapter<LogoAdapter.LogoHolder>() {

    val inflater = LayoutInflater.from(context)

    class LogoHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var binding: ItemLogoBinding? = null
    }

    fun addDatas(list: List<Brand>?) {
        if (list != null) {
            mData.addAll(list)
            notifyDataSetChanged()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LogoHolder {
        val binding: ItemLogoBinding =
            DataBindingUtil.inflate(inflater, R.layout.item_logo, parent, false)
        var holder = LogoHolder(binding.root)
        holder.binding = binding
        return holder
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    override fun onBindViewHolder(holder: LogoHolder, position: Int) {
        val bean = mData.get(position)
        holder.binding?.item = bean
        holder.binding?.root?.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                context.startActivity(Intent(context, BrandListActivity::class.java)
                    .apply {
                        putExtra("logoBean", bean)
                    })
            }
        })
    }
}